load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_python//python:defs.bzl", "py_library")
load("@py_deps//:requirements.bzl", "requirement")
load("@rules_python//experimental/python:wheel.bzl", "py_package", "py_wheel")
load("//:defs.bzl", "anki_version")

genrule(
    name = "hooks_gen",
    outs = ["hooks_gen.py"],
    cmd = "$(location //qt:genhooks_gui) $@",
    tools = ["//qt:genhooks_gui"],
)

genrule(
    name = "extract_sass_colors",
    srcs = [
        "//ts/sass:_vars.scss",
    ],
    outs = ["colors.py"],
    cmd = "$(location //qt:extract_sass_colors) $< $@",
    tools = [
        "//qt:extract_sass_colors",
    ],
)

_py_srcs = glob(
    [
        "**/*.py",
    ],
    exclude = ["hooks_gen.py"],
)

_py_srcs_and_forms = _py_srcs + [
    "//qt/aqt/forms:forms",
    "//qt/aqt/forms:icons",
]

aqt_core_data = [
    "colors.py",
    "py.typed",
    ":hooks_gen",
]

aqt_deps = [
    requirement("protobuf"),
    requirement("decorator"),
    requirement("requests"),
    requirement("beautifulsoup4"),
    requirement("flask"),
    requirement("flask-cors"),
    requirement("waitress"),
    requirement("send2trash"),
    requirement("markdown"),
    requirement("jsonschema"),
    "@pyqt5//:pkg",
] + select({
    "@bazel_tools//src/conditions:host_windows": [
        requirement("psutil"),
        requirement("pywin32"),
        requirement("winrt"),
    ],
    "//conditions:default": [],
})

# library without web/i18n data; faster for testing
py_library(
    name = "aqt_without_data",
    srcs = _py_srcs_and_forms,
    data = aqt_core_data,
    visibility = ["//visibility:public"],
    deps = aqt_deps,
)

py_library(
    name = "aqt_with_data",
    srcs = _py_srcs_and_forms,
    data = aqt_core_data + ["//qt/aqt/data"],
    visibility = ["//visibility:public"],
    deps = aqt_deps,
)

py_package(
    name = "aqt_pkg",
    packages = [
        "qt.aqt",
    ],
    deps = [
        ":aqt_with_data",
    ],
)

py_wheel(
    name = "wheel",
    description_file = "wheel_description.txt",
    distribution = "aqt",
    entry_points = {
        "console_scripts": ["anki = aqt:run"],
    },
    homepage = "https://apps.ankiweb.net",
    license = "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
    python_tag = "py3",
    python_version = ">=3.8",
    requires = [
        "beautifulsoup4",
        "requests",
        "send2trash",
        "markdown",
        "jsonschema",
        "flask",
        "flask_cors",
        "waitress",
        "pyqt5>=5.12",
        "pyqtwebengine",
        'psutil; sys.platform == "win32"',
        'pywin32; sys.platform == "win32"',
        'winrt==1.0.20239.1; sys.platform == "win32" and platform_release == "10" and python_version == "3.8"',
        'winrt; sys.platform == "win32" and platform_release == "10" and python_version >= "3.9"',
        "anki==" + anki_version,
    ],
    strip_path_prefixes = [
        "qt/",
    ],
    tags = ["manual"],
    version = anki_version,
    visibility = ["//visibility:public"],
    deps = [
        ":aqt_pkg",
    ],
)

filegroup(
    name = "py_source_files",
    srcs = _py_srcs,
    visibility = [
        "//qt:__subpackages__",
    ],
)
